package com.shinewind.info.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.info.mapper.IUserFollowMapper;
import com.shinewind.info.service.IUserFollowService;
import com.shinewind.pojo.dto.PageDto;
import com.shinewind.pojo.entity.UserFollow;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserFollowServiceImpl extends ServiceImpl<IUserFollowMapper, UserFollow> implements IUserFollowService {
    @Override
    public List<String> getfIdByuId(String uId,PageDto pageDto) {
        List<UserFollow> userFollows = this.lambdaQuery().eq(UserFollow::getUId,uId)
                .page(new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize())).getRecords();
        //按id查询所有被关注的用户的id
        List<String> fIdList = userFollows.stream().map(userFollow -> {
            return userFollow.getFId();
        }).collect(Collectors.toList());
        return fIdList;
    }

    @Override
    public List<String> getuIdByfId(String fId) {
        List<UserFollow> userFollows = this.lambdaQuery().eq(UserFollow::getFId,fId).list();
        //按id查询所有被关注的用户的id
        List<String> uIdList = userFollows.stream().map(userFollow -> {
            return userFollow.getUId();
        }).collect(Collectors.toList());
        return uIdList;
    }

    @Override
    public Long getfIdCount(String uId) {
        Long total = this.lambdaQuery().eq(UserFollow::getUId,uId).count();
        return total;
    }

    @Override
    public String focusUserByFidANDUid(String fId, String uId) {
        System.out.println("fId:"+fId);
        System.out.println("uId:"+uId);
        if(!fId.equals(uId)){
            //如果该用户已经被当前登录的用户关注
            if(this.lambdaQuery().eq(UserFollow::getFId,fId).eq(UserFollow::getUId,uId).count() > 0){
                return "你已经关注该用户了,请不要重复操作";
            }
            else if(this.lambdaQuery().eq(UserFollow::getFId,fId).eq(UserFollow::getUId,uId).count() == 0){
                //未关注,则关注该用户
                UserFollow userFollow = UserFollow.builder().fId(fId).uId(uId).build();
                return this.save(userFollow) == true ? "关注成功" : "关注失败";
            }
        }
        else{
            //禁止自己关注自己
            return "请不要关注自己";
        }
        return "未知错误";
    }

    @Override
    public String cancelFocusUserByFidANDUid(String fId, String uId) {
        if(!fId.equals(uId)){
            //如果该用户在表中有记录
            if(this.lambdaQuery().eq(UserFollow::getFId,fId).eq(UserFollow::getUId,uId).count() > 0){
                //删除
                //未关注,则关注该用户
                UserFollow userFollow = UserFollow.builder().fId(fId).uId(uId).build();
                return this.lambdaUpdate().eq(UserFollow::getFId,fId).eq(UserFollow::getUId,uId).remove() == true
                        ? "取消关注成功" : "取消关注失败";
            }
        }
        return "未知错误";
    }
}
